<style lang="scss">
	.index-header {
		padding: 30rpx 30rpx;
		width: 100%;
		box-sizing: border-box;
		display: flex;
		// 设置子元素两端对齐
		justify-content: space-between;
		align-items: center;
		border-bottom: 1px solid #f0f0f0;
		background-color: #00BFFF;
		color: #fff;
		.left {
			font-weight: bold;
			font-size: 18px;
		}
	}
</style>
<template>
	<view class="index">
		<!-- LOGO |  跳转个人中心的链接 -->
		<view class="index-header">
			<view class="left">
				<text>掌上刷题宝</text>
			</view>
			<view class="right">
				 <navigator url="/pages/my/my" v-if="info.nickname">
					 {{info.nickname}}
				 </navigator>
				 <navigator url="/pages/login/login" v-else>
					 登录
				 </navigator>
			</view>
		</view>
		
		
		<!-- 1.0 轮播图 -->
		<banner :arr="banners"></banner>
		
		<!-- 2.0 一级分类 -->
		<category :arr="categories"></category>
		
		<!-- 3.0 签到 -->
		<sigin></sigin>
		
		<!-- 4.0 搜索 -->
		<search></search>
		
		<!-- 5.0 新闻动态列表 -->
		<news_list :arr="newsArray"></news_list>
	</view>
</template>

<script setup>
	import { reactive, ref } from 'vue';
	
	// 导入用户、一级菜单的集合对象
	import { usersCollection ,categorysCollection } from '../../common/db/index.js';
	
	// 导入组件  （注册组件）
	import banner from '../../components/banner/banner.vue';
	import category from '../../components/category/category.vue';
	import sigin from '../../components/sigin/sigin.vue';
	import search from '../../components/search/search.vue';
	import news_list from '../../components/news_list/news_list.vue';
	
	// 定义数组 记录轮播图数据
	const banners = ref([
		{
			src:"../../static/b1.jpg",
			id: 1
		},
		{
			src:"../../static/b2.jpeg",
			id: 2
		}
	])
	
	// 定义数组 记录一级分类
	const categories = ref([])
	
	// 定义数组 记录动态列表
	const newsArray = ref([
		{
			title: "标题111",
			date: "2024-12-12",
			id: 1
		},
		{
			title: "标题222",
			date: "2024-12-11",
			id: 2
		},
		{
			title: "标题333",
			date: "2024-12-10",
			id: 3
		}
	])

	// 定义用户对象
	const info = reactive({})

	// 获取用户身份的方法
	const getUserInfo = async ()=> {
		let user_id = uni.getStorageSync("ZSSTB_ID");
		console.log(user_id);
		if(user_id){
			await usersCollection.where({_id: user_id}).get()
			.then(
				res=> {
					// console.log("查询结果:",res);
					let result = res.result;
					if(result.data.length == 1) {
						let  {email , createAt, nickname, register_date} = result.data[0];
						info.email = email;
						info.nickname = nickname;
						info.register_date = register_date;
					}
				}
			)
		}
	}
	
	// 编写方法获取一级菜单
	const listCategory = async ()=> {
		uni.showLoading({
			title:"正在加载..."
		})
		await  categorysCollection.get()
		.then(res=>{
			console.log(res);
			let result = res.result;
			// 赋值给categories变量
			categories.value = result.data;
		})
		.catch(err=> console.log(err))
		uni.hideLoading();
	}
	
	
	// 初始化
	// 调用方法
	getUserInfo();
	listCategory();
</script>